package cn.tqfeiyang.smartadmin.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.pagination.QueryParams;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.system.request.RoleCreateDTO;
import cn.tqfeiyang.smartadmin.system.request.RoleQueryDTO;
import cn.tqfeiyang.smartadmin.system.request.RoleUpdateDTO;
import cn.tqfeiyang.smartadmin.system.response.*;
import cn.tqfeiyang.smartadmin.system.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 角色控制器
 *
 * @author tqfeiyang
 * @since 2024/8/20 11:17
 */
@RestController
@Validated
@RequestMapping("/api/system/role")
@Tag(name = "系统/角色")
public class RoleController {

    /**
     * 角色服务
     */
    @Resource
    private RoleService roleService;

    /**
     * 查询角色
     *
     * @param dto 请求数据
     * @return 角色列表
     */
    @OperationLog
    @GetMapping("/query")
    @Operation(summary = "查询角色")
    @SaCheckPermission("system:role:query")
    public ApiResult<PageData<RoleQueryVO>> queryRoles(@Validated RoleQueryDTO dto) {
        return ApiResult.success(roleService.queryRoles(dto));
    }

    /**
     * 获取角色
     *
     * @param roleId 角色id
     * @return 角色信息
     */
    @OperationLog
    @GetMapping("/{roleId}/read")
    @Operation(summary = "获取角色")
    @SaCheckPermission("system:role:read")
    public ApiResult<RoleDetailVO> readRole(@PathVariable @EntityId Long roleId) {
        return ApiResult.success(roleService.readRole(roleId));
    }

    /**
     * 添加角色
     *
     * @param dto 请求数据
     * @return 角色信息
     */
    @OperationLog
    @PostMapping("/create")
    @Operation(summary = "添加角色")
    @SaCheckPermission("system:role:create")
    public ApiResult<RoleDetailVO> createRole(@RequestBody @Validated RoleCreateDTO dto) {
        Long roleId = roleService.createRole(dto);
        return ApiResult.success(roleService.readRole(roleId));
    }

    /**
     * 修改角色
     *
     * @param roleId 角色id
     * @param dto 请求数据
     * @return 角色信息
     */
    @OperationLog
    @PostMapping("/{roleId}/update")
    @Operation(summary = "修改角色")
    @SaCheckPermission("system:role:update")
    public ApiResult<RoleDetailVO> updateRole(@PathVariable @EntityId Long roleId, @RequestBody @Validated RoleUpdateDTO dto) {
        roleService.updateRole(roleId, dto);
        return ApiResult.success(roleService.readRole(roleId));
    }

    /**
     * 删除角色
     *
     * @param roleId 角色id
     */
    @OperationLog
    @PostMapping("/{roleId}/delete")
    @Operation(summary = "删除角色")
    @SaCheckPermission("system:role:delete")
    public ApiResult<String> deleteRole(@PathVariable @EntityId Long roleId) {
        roleService.deleteRole(roleId);
        return ApiResult.success();
    }

    /**
     * 查询菜单
     *
     * @param roleId 角色id
     */
    @OperationLog
    @GetMapping("/{roleId}/menus/query")
    @Operation(summary = "查询菜单")
    @SaCheckPermission("system:role:menus:query")
    public ApiResult<PageData<MenuQueryVO>> queryMenus(@PathVariable @EntityId Long roleId, @Validated QueryParams dto) {
        return ApiResult.success(roleService.queryMenus(roleId, dto));
    }

    /**
     * 修改菜单
     *
     * @param roleId 角色id
     * @param menuIds 菜单id集合
     */
    @OperationLog
    @PostMapping("/{roleId}/menus/update")
    @Operation(summary = "修改菜单")
    @SaCheckPermission("system:role:menus:update")
    public ApiResult<String> updateMenus(@PathVariable @EntityId Long roleId, @RequestBody Set<Long> menuIds) {
        roleService.updateMenus(roleId, menuIds);
        return ApiResult.success();
    }

    /**
     * 查询权限
     *
     * @param roleId 角色id
     */
    @OperationLog
    @GetMapping("/{roleId}/permissions/query")
    @Operation(summary = "查询权限")
    @SaCheckPermission("system:role:permissions:query")
    public ApiResult<PageData<PermissionQueryVO>> queryPermissions(@PathVariable @EntityId Long roleId, @Validated QueryParams dto) {
        return ApiResult.success(roleService.queryPermissions(roleId, dto));
    }

    /**
     * 修改权限
     *
     * @param roleId 角色id
     * @param menuIds 权限id集合
     */
    @OperationLog
    @PostMapping("/{roleId}/permissions/update")
    @Operation(summary = "修改权限")
    @SaCheckPermission("system:role:permissions:update")
    public ApiResult<String> updatePermissions(@PathVariable @EntityId Long roleId, @RequestBody Set<Long> menuIds) {
        roleService.updatePermissions(roleId, menuIds);
        return ApiResult.success();
    }

    /**
     * 查询用户
     *
     * @param roleId 角色id
     */
    @OperationLog
    @GetMapping("/{roleId}/users/query")
    @Operation(summary = "查询用户")
    @SaCheckPermission("system:role:users:query")
    public ApiResult<PageData<UserQueryVO>> queryUsers(@PathVariable @EntityId Long roleId, @Validated QueryParams dto) {
        return ApiResult.success(roleService.queryUsers(roleId, dto));
    }

    /**
     * 修改用户
     *
     * @param roleId 角色id
     * @param userIds 用户id集合
     */
    @OperationLog
    @PostMapping("/{roleId}/users/update")
    @Operation(summary = "修改用户")
    @SaCheckPermission("system:role:users:update")
    public ApiResult<String> updateUsers(@PathVariable @EntityId Long roleId, @RequestBody Set<Long> userIds) {
        roleService.updateUsers(roleId, userIds);
        return ApiResult.success();
    }
}
